var webchat={
    //g_api:"http://localhost:8083",    //dev
    g_api:"",
    g_host:function(){
        //return "ws://localhost:8083"; //dev
        return window.location.host;
    },
    g_ws:function(){
        return "ws://"+webchat.g_host();
    },
    g_usr:null,
    setCookie:function(c_name,value,expire){
              var date = new Date();
              date.setTime(date.getTime()+expire*24*60*60*1000);
              document.cookie = c_name+'='+escape(value)+'; expires='+date.toGMTString();
     },
    getCookie:function (c_name)
    {
        if(document.cookie.length>0){
            let c_start = document.cookie.indexOf(c_name+'=')
            if(c_start!=-1){
                c_start = c_start+c_name.length+1
                let c_end = document.cookie.indexOf(';',c_start)
                if(c_end == -1) c_end=document.cookie.length
                return unescape(document.cookie.substring(c_start,c_end))
            }
        }
        return ""
    },
    relogin:function(){
            alert("请重新登陆");
            window.location.href = '/login.html';
    },
    getuser:function(fn){
        let token = webchat.getCookie("token");
        if (token==null||token==""||token=="undefined"){
            alert("请重新登陆");
            window.location.href = '/login.html';
        }
        if(webchat.g_usr!=null){
            fn(g_usr);
            return;
        }
        $.post(webchat.g_api+"/user/info",{token:token},function(resp){
            if(resp!=null&&resp.error == null&&resp.result!=null){
                webchat.g_usr = resp.result;
                fn(resp.result);
            }else{
                webchat.relogin();
            }
        });
    },
    login:function(username,pwd,fn){
        $.post(webchat.g_api+"/user/login",{username:username,pwd:pwd},function (rspj) {
                //var rspj = JSON.parse(resp)
                if (rspj!=null&& rspj.error==null&&rspj.result!=null&&rspj.result.token!=null){
                    webchat.setCookie("token",rspj.result.token,10);
                    window.location.href = '/index.html';
                }else{
                    if(rspj.error!=null&& rspj.error.msg!=null)
                        alert(rspj.error.msg);
                    fn();
                }
            });
    },
    getrooms:function(){

    },
    exist:function (username,fn){
        $.post(webchat.g_api+"/user/exist",{name:username},function(resp){
            if(resp==null||resp.result==null||resp.result){
                fn(true);
            }else{
                fn(false);
            }
        });
    },
    signup:function(username,nickname,password,fn){
            $.post(webchat.g_api+"/user/signup",{username:username,nickname:nickname,password:password},function(resp){
                if(resp!=null&&resp.result){
                    fn(true);
                }else{
                    fn(false);
                }
             });
    },
    queryRoom:function (txt,fn) {
        $.post(webchat.g_api+"/room/query",{token:webchat.g_usr.token,title:txt},function(resp){
            if(resp!=null&&resp.result){
                fn(resp.result);
            }else{
                fn(null);
            }
        });
    },
    createRoom:function (txt,fn) {
        $.post(webchat.g_api+"/room/create/"+txt,{token:webchat.g_usr.token},function(resp){
            if(resp!=null&&resp.result){
                fn(resp.result);
            }else{
                fn(null);
            }
        });
    },
    ws:null,
    wsdata:null,
    joinRoom:function (roomid,dataobj,callback) {
        var ws=new WebSocket(webchat.g_ws()+"/roomws/"+roomid+"/"+webchat.g_usr.token);
        if(webchat.ws!=null){
            webchat.ws.close();
            webchat.ws.isAbandon = true;
        }
        webchat.ws = ws;
        webchat.wsdata=dataobj;
        ws.onopen = function()
        {
            // Web Socket 已连接上，使用 send() 方法发送数据
            //callback( "onopen");
            //ws.send(JSON.stringify({username:webchat.g_usr.username,roomId:roomid}));
        };

        ws.onmessage = function (evt)
        {
            var received_msg = evt.data;
            dataj = JSON.parse(received_msg);
            callback( dataj.speaker+":"+dataj.text);

            // ws.send("发送数据2"+i);
        };

        ws.onclose = function()
        {
            if(ws.isAbandon){
                return;
            }
            callback( "与服务器断开连接");

            //webchat.wsdata.lines.push( "连接已关闭");
            // 关闭 websocket
            //alert("连接已关闭...");
        };
    },
    sendMessage:function(msg){
        webchat.ws.send(msg);
    }

}


//module.exports.webchat=webchat